package com.firebird.app.fekpsl.ui.activity.user

import com.firebird.app.fekpsl.R
import android.content.Intent
import android.view.View
import com.firebird.app.fekpsl.base.BaseMvpActivity
import com.firebird.app.fekpsl.constant.Constant
import com.firebird.app.fekpsl.event.LoginEvent
import com.firebird.app.fekpsl.ext.showToast
import com.firebird.app.fekpsl.mvp.user.RegisterContract
import com.firebird.app.fekpsl.bean.LoginData
import com.firebird.app.fekpsl.mvp.user.RegisterPresenter
import com.firebird.app.fekpsl.utils.DialogUtil
import com.firebird.app.fekpsl.utils.Preference
import kotlinx.android.synthetic.main.user_register.*
import kotlinx.android.synthetic.main.toolbar.*
import org.greenrobot.eventbus.EventBus

class RegisterActivity : BaseMvpActivity<RegisterContract.View, RegisterContract.Presenter>(), RegisterContract.View {

    /**
     * local username
     */
    private var pf_user: String by Preference(Constant.USERNAME_KEY, "")
    /**
     * local password
     */
    private var pf_pwd: String by Preference(Constant.PASSWORD_KEY, "")
    private var pf_token: String by Preference(Constant.TOKEN_KEY, "")
    private var pf_uuid:String by Preference(Constant.UUID_KEY, "")
    private var  pf_nicename:String by Preference(Constant.NICE_NAME_KEY, "")
    private var pf_realname:String by Preference(Constant.REAL_NAME_KEY, "")
    private var  pf_sex:Int by Preference(Constant.SEX_KEY, 1)
    private var  pf_birthday:String by Preference(Constant.BIRTHDAY_KEY, "")
    private var  pf_phone:String by Preference(Constant.PHONE_KEY, "")
    private var  pf_avatar:String by Preference(Constant.AVATAR_KEY, "")
    private var  pf_userid:String by Preference(Constant.USER_ID_KEY, "")
    private var  pf_email:String by Preference(Constant.EMAIL_KEY, "")
    private var  pf_creditscore:Int by Preference(Constant.CREDIT_SCORE_KEY, 100)
    private var  pf_area:String by Preference(Constant.AREA_KEY, "")
    private var  pf_address:String by Preference(Constant.ADDRESS_KEY, "")
    private var  pf_isexplicit:Int by Preference(Constant.IS_EXPLICIT_KEY, 1)
    private var  pf_coppercash:Long by Preference(Constant.COPPER_CASH_KEY, 5000)
    private var  pf_goldingot:Long by Preference(Constant.GOLD_INGOT_KEY, 0)
    private var  pf_levelscore:Long by Preference(Constant.LEVEL_SCORE_KEY, 0)

    override fun createPresenter(): RegisterContract.Presenter = RegisterPresenter()

    private val mDialog by lazy {
        DialogUtil.getWaitDialog(this, getString(R.string.register_ing))
    }

    override fun showLoading() {
        mDialog.show()
    }

    override fun hideLoading() {
        mDialog.dismiss()
    }

    override fun registerSuccess(data: LoginData) {
        val myuuid=data.user_uuid
        if (myuuid.length>5) {
            isLogin = true
            pf_user = data.user_name
            //pwd = data.password
            pf_token = data.token
            pf_uuid = data.user_uuid
            pf_nicename = data.nice_name
            pf_realname = data.real_name
            pf_sex = data.sex
            pf_birthday = data.birth_day
            pf_phone = data.phone
            pf_avatar = data.avatar_text
            pf_userid = data.user_id
            pf_email = data.email
            pf_creditscore = data.credit_score
            pf_area = data.area
            pf_address = data.address
            pf_isexplicit = data.is_explicit
            pf_coppercash = data.copper_cash
            pf_goldingot = data.gold_ingot
            pf_levelscore = data.level_score
            isLogin = true
            EventBus.getDefault().post(LoginEvent(true))
            finish()
        }else{
            isLogin = false
            showToast(getString(R.string.register_fail))
        }
    }

    override fun registerFail() {
        isLogin = false
    }

    override fun attachLayoutRes(): Int = R.layout.user_register

    override fun useEventBus(): Boolean = false

    override fun enableNetworkTip(): Boolean = false

    override fun initData() {
    }

    override fun initView() {
        super.initView()

        toolbar.run {
            title = resources.getString(R.string.register)
            setSupportActionBar(this)
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
        }
        btn_register.setOnClickListener(onClickListener)
        tv_sign_in.setOnClickListener(onClickListener)
    }

    override fun start() {
    }

    /**
     * OnClickListener
     */
    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.btn_register -> {
                register()
            }
            R.id.tv_sign_in -> {
                Intent(this@RegisterActivity, LoginActivity::class.java).apply {
                    startActivity(this)
                }
                finish()
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
            }
        }
    }

    /**
     * Register
     */
    private fun register() {
        if (validate()) {
            mPresenter?.registerFejpslAndroid(et_username.text.toString(),
                    et_password.text.toString()
            )
        }
    }

    /**
     * check data
     */
    private fun validate(): Boolean {
        var valid = true
        val username: String = et_username.text.toString()
        val password: String = et_password.text.toString()
        val password2: String = et_password2.text.toString()
        if (username.isEmpty()) {
            et_username.error = getString(R.string.username_not_empty)
            valid = false
        }
        if (password.isEmpty()) {
            et_password.error = getString(R.string.password_not_empty)
            valid = false
        }
        if (password2.isEmpty()) {
            et_password2.error = getString(R.string.confirm_password_not_empty)
            valid = false
        }
        if (password != password2) {
            et_password2.error = getString(R.string.password_cannot_match)
            valid = false
        }
        return valid
    }

}
